package com.example.planetexploder.module.edittext;

import android.content.Context;
import android.util.Log;

import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.model.dao.Dao;
import com.example.planetexploder.model.folder.Folder;
import com.example.planetexploder.model.preference.SharedPreference;
import com.example.planetexploder.utils.TimeUtil;

/**
 * 文本编辑页面Presenter
 * @author 蒋子涵
 * 修改日期：2019.10.22
 */
public class Presenter_EditText {

    private static final String TAG = "Presenter_EditText";

    private Dao dao;
    private Folder folder;
    private String currentTime;
    private ItemInfo itemInfo;
    private BookInfo bookInfo;
    private SharedPreference sp;

    public Presenter_EditText(Context context,BookInfo bookInfo,ItemInfo itemInfo)
    {
        dao = new Dao(context);
        folder = new Folder(context);
        this.bookInfo = bookInfo;
        this.itemInfo = itemInfo;
        this.currentTime = TimeUtil.getCurrentTime();
        this.sp = new SharedPreference(context);
    }

    /**
     * 获得当前页面item已储存的文本
     * @return 文本字符串
     * @throws Exception
     */
    public String get_Text() throws Exception {
        Log.d(LogConst.PTAG, "get_Text()");
        if(itemInfo.itemId == null)
            return "";
        else
            return folder.textFile.get_Text(bookInfo.bookId,itemInfo.itemId);
    }

    /**
     * 保存文本摘抄，点勾或退出按钮时调用
     * @param text 文本信息
     */
    public void save_Text(String text) throws Exception {
        Log.d(LogConst.PTAG, "save_Text()");
        //点击新建摘抄
        if(itemInfo.itemId == null)
        {
            //结果获得的字符串为空，丢弃新建的摘抄
            if(text == null)
                return;
            //设置该项文本摘抄的name，去掉空格和回车
            String cut_blank;
            cut_blank = text.replace(" ", "");
            cut_blank = cut_blank.replace("\n", "");
            itemInfo.itemName = cut_blank.length() > 20 ? cut_blank.substring(0, 19) : cut_blank;

            //更改该项文本摘抄和摘抄集的信息
            itemInfo.itemId = ItemInfo.textIdHead + currentTime;
            itemInfo.dateMod = currentTime;
            bookInfo.dateMod = currentTime;

            //保存和更新数据库，保存文件
            dao.bookTable.add_ItemInfo_To_BookTable(bookInfo.bookId,itemInfo);
            dao.shelfTable.update_BookInfo(bookInfo);
            folder.textFile.save_Text(bookInfo.bookId,itemInfo.itemId,text);

        }

        //点击已有摘抄
        else
        {
            //结果获得的字符串为空，删除摘抄
            if(text == null)
            {
                dao.bookTable.delete_ItemInfo_From_BookTable(bookInfo.bookId,itemInfo.itemId);
                folder.textFile.delete_Text(bookInfo.bookId,itemInfo.itemId);
                return;
            }
            //设置该项文本摘抄的name，去掉空格和回车
            String cut_blank;
            cut_blank = text.replace(" ", "");
            cut_blank = cut_blank.replace("\n", "");
            itemInfo.itemName = cut_blank.length() > 20 ? cut_blank.substring(0, 19) : cut_blank;

            //更新摘抄和摘抄集的修改时间
            itemInfo.dateMod = currentTime;
            bookInfo.dateMod = currentTime;

            //保存和更新数据库，保存文件
            dao.bookTable.update_ItemInfo(bookInfo.bookId,itemInfo);
            dao.shelfTable.update_BookInfo(bookInfo);
            folder.textFile.save_Text(bookInfo.bookId,itemInfo.itemId,text);
        }
        //将修改过的摘抄集设置为首页摘抄集
        sp.appSettings.set_FirstPageBook(bookInfo.bookId);
    }
}
